package gui;

import gui.threads.SearchThread;

import java.sql.ResultSet;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import DBside.JDBCConnection;
import LogicSide.ManageSongs;
import LogicSide.client;

public class Search {
	private static int labelStyle = SWT.CENTER;
	private static int textBoxStyle = SWT.SINGLE | SWT.BORDER;

	private static Label songNameLabel;
	private static Text songNameText;

	private static Label albumNameLabel;
	private static Text albumNameText;

	private static Label artistNameLabel;
	private static Text artistNameText;

	private static Label genreNameLabel;
	private static Text genreNameText;

	private static Table songsTable;
	
	private static int selected_row_num = -1;
	private static TableItem selected_item = null;
	private static int selected_id = 0;
	private static String selected_name= "";

	public static Composite createSearch(final Shell shell, ResultSet resultSet) {
		Main.inSearch  = true;
		// Construct the main search panel
		Composite search = new Composite(shell, SWT.NONE);

		// search.setLayout(new GridLayout(1, false));
		search.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		// RowLayout searchLayout = new RowLayout(SWT.VERTICAL);
		// searchLayout.fill = true;
		GridLayout searchLayout = new GridLayout(1, false);

		// FillLayout searchLayout = new FillLayout(SWT.VERTICAL);
		search.setLayout(searchLayout);

		// Construct the search option panel (where all the search text fields
		// and buttons are)
		Composite searchOptions = new Composite(search, SWT.BORDER);
		searchOptions.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_MAGENTA));
		searchOptions.setLayout(getSearchLayout());
		searchOptions.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false,
				false, 1, 1));
		//

		// Construct the search result panel (where the result will be)
		final Composite searchResult = new Composite(search, SWT.BORDER);
		searchResult.setLayout(new GridLayout());
		searchResult.setBackground(Display.getCurrent().getSystemColor(
				SWT.COLOR_MAGENTA));
		searchResult.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true,
				1, 1));
		//

		// Search options:
		songNameLabel = addSearchOptionLabel("Song name", searchOptions);
		songNameLabel.setBackground(Display.getCurrent().getSystemColor(
				SWT.COLOR_WHITE));
		songNameText = addSearchOptionTextBox("Song name", searchOptions);
		songNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				true, 1, 1));

		albumNameLabel = addSearchOptionLabel("Album name", searchOptions);
		albumNameLabel.setBackground(Display.getCurrent().getSystemColor(
				SWT.COLOR_WHITE));
		albumNameText = addSearchOptionTextBox("Album name", searchOptions);
		albumNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				true, 1, 1));

		artistNameLabel = addSearchOptionLabel("Artist name", searchOptions);
		artistNameLabel.setBackground(Display.getCurrent().getSystemColor(
				SWT.COLOR_WHITE));
		artistNameText = addSearchOptionTextBox("Artist name", searchOptions);
		artistNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				true, 1, 1));

		genreNameLabel = addSearchOptionLabel("Genre name", searchOptions);
		genreNameLabel.setBackground(Display.getCurrent().getSystemColor(
				SWT.COLOR_WHITE));
		genreNameText = addSearchOptionTextBox("Genre name", searchOptions);
		genreNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				true, 1, 1));

		// Add search button
		Button searchButton = new Button(searchOptions, SWT.PUSH | SWT.FLAT);
		searchButton.setText("Search !");
		searchButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				Utils.loadingCursor(Main.display, shell);
				SearchThread searchThread = new SearchThread(songNameText
						.getText(), artistNameText.getText(), albumNameText
						.getText(), genreNameText.getText(), shell);
				searchThread.start();
			}
		});
		searchButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				true, 2, 1));

		// Add buttons to searchAction composite
		Button addSong = new Button(searchOptions, SWT.PUSH | SWT.FLAT);
		addSong.setText("Add song");
		addSong.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, 2,
				1));
		addSong.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				Thread thread = new Thread(new Runnable() {
					
					@Override
					public void run() {
						final boolean success = ManageSongs.addSongToUser(selected_id, selected_name, Main.session);
						Main.display.asyncExec(new Runnable() {
							
							@Override
							public void run() {
								if(success){
									Utils.popUpBox("Success", "Song added successfully", shell);
								}
								else{
									Utils.popUpBox("Failure", "Couldn't add song", shell);
								}
								
							}
						});
						}
				});
				thread.start();
			}
		});
		
		

		// intialize table data with result-set table
		final Table resultTable = client.selectPlayList(resultSet, searchResult);
		resultTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true,
				1, 1));
		songsTable = resultTable;
		resultTable.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				selected_row_num = resultTable.getSelectionIndex();
				selected_item = resultTable.getItem(selected_row_num);
				selected_id = Integer.parseInt(selected_item.getText());
				selected_name = selected_item.getText(1);
			}
		});
		
		JDBCConnection.close(resultSet, null);

		return search;
	}

	private static Label addSearchOptionLabel(String option, Composite composite) {
		Label optionLabel = new Label(composite, labelStyle);
		optionLabel.setText(option + ":");
		return optionLabel;
	}

	private static Text addSearchOptionTextBox(String option,
			Composite composite) {
		Text optionTextBox = new Text(composite, textBoxStyle);
		return optionTextBox;
	}

	private static Layout getSearchLayout() {
		GridLayout layout = new GridLayout(4, false);
		return layout;
	}

}